package com.huitong.admin.system.controller;

import com.huitong.admin.system.domain.dto.StudentDTO;
import com.huitong.admin.system.domain.dto.TeacherDTO;
import com.huitong.admin.system.domain.dto.UserPageDTO;
import com.huitong.admin.system.domain.vo.UserVO;
import com.huitong.admin.system.mapper.UserMapper;
import com.huitong.admin.system.service.UserService;
import com.huitong.common.core.annotation.Id;
import com.huitong.common.core.constant.UserConstant;
import com.huitong.common.core.domain.Result;
import com.huitong.common.core.exception.UserException;
import com.huitong.common.mybatis.domain.PageResult;
import jakarta.validation.constraints.Pattern;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.huitong.common.core.constant.ValidateConstant.ROLE_NO_ADMIN_REGEX;
import static com.huitong.common.core.enums.IdType.USER_ID;

/**
 * 用户信息控制层
 *
 * @author fromdrowning
 * @date 2024/5/10 17:39
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * 用户信息服务层
     */
    private final UserService userService;

    /**
     * 用户信息数据包访问层
     */
    private final UserMapper userMapper;

    /**
     * 新增学生信息
     *
     * @param studentDTO 学生信息
     * @return {@link Result }<{@link Void }> 状态码200 “操作成功”
     */
    @PostMapping("/student/insert")
    public Result<Void> insertStudent(@Validated @RequestBody StudentDTO studentDTO) {
        // 先判断学号是否存在再开启事务
        verifyHasUser(studentDTO.getId());
        userService.insertStudent(studentDTO);
        return Result.success();
    }

    /**
     * 新增教师信息
     *
     * @param teacherDTO 教师信息
     * @return {@link Result }<{@link Void }> 状态码200 “操作成功”
     */
    @PostMapping("/teacher/insert")
    public Result<Void> insertTeacher(@Validated @RequestBody TeacherDTO teacherDTO) {
        // 先判断学号是否存在再开启事务
        verifyHasUser(teacherDTO.getId());
        userService.insertTeacher(teacherDTO);
        return Result.success();
    }

    /**
     * 查询用户信息
     *
     * @param id   学号
     * @param role 角色
     * @return {@link Result }<{@link UserVO }> 用户信息
     */
    @GetMapping("/select")
    public Result<UserVO> selectUser(@Id(type = USER_ID) Long id, @Pattern(regexp = ROLE_NO_ADMIN_REGEX) String role) {
        return userService.selectUser(id, role);
    }

    /**
     * 查询用户信息
     *
     * @param userPageDTO 分页信息
     * @return {@link PageResult }<{@link UserVO }> 分页信息
     */
    @GetMapping("/page")
    public PageResult<UserVO> pageUser(@Validated UserPageDTO userPageDTO) {
        return userService.pageUser(userPageDTO);
    }

    /**
     * 修改学生信息
     *
     * @param studentDTO 学生信息
     * @return {@link Result}<{@link Void}> 状态码200 “操作成功”
     */
    @PutMapping("/student/update")
    public Result<Void> updateStudent(@Validated @RequestBody StudentDTO studentDTO) {
        userService.updateStudent(studentDTO);
        return Result.success();
    }

    /**
     * 修改教师信息
     *
     * @param teacherDTO 学生信息
     * @return {@link Result}<{@link Void}> 状态码200 “操作成功”
     */
    @PutMapping("/teacher/update")
    public Result<Void> updateTeacher(@Validated @RequestBody TeacherDTO teacherDTO) {
        userService.updateTeacher(teacherDTO);
        return Result.success();
    }

    /**
     * 删除用户信息
     *
     * @param id   学号
     * @param role 角色
     * @return {@link Result}<{@link Void}> 状态码200 “操作成功”
     */
    @DeleteMapping("/delete/{id}/{role}")
    public Result<Void> deleteUser(@Id(type = USER_ID) @PathVariable Long id,
                                   @Pattern(regexp = ROLE_NO_ADMIN_REGEX) @PathVariable String role) {
        userService.deleteUser(id, role);
        return Result.success();
    }

    /**
     * 校验student admin teacher三个表中是否存在该学号
     *
     * @param id 学号
     */
    private void verifyHasUser(Long id) {
        if (userMapper.hasUser(id, UserConstant.STUDENT) > 0 ||
            userMapper.hasUser(id, UserConstant.TEACHER) > 0 ||
            userMapper.hasUser(id, UserConstant.ADMIN) > 0) {
            throw new UserException("该学号已存在");
        }
    }
}
